package com.wc.alorithm_luogu._BFS.回家;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/4/11 16:58
 * @description https://www.luogu.com.cn/problem/P2802
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 15;
    static int[][] g = new int[N][N];
    static int[][] dist = new int[N][N];
    static int[][] ghp = new int[N][N];
    static int n, m, sx, sy, ex, ey;
    static int[] dx = new int[]{-1, 1, 0, 0};
    static int[] dy = new int[]{0, 0, -1, 1};

    public static void main(String[] args) {
        n = sc.nextInt();
        m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                g[i][j] = sc.nextInt();
                if (g[i][j] == 2) {
                    sx = i;
                    sy = j;
                }
                if (g[i][j] == 3) {
                    ex = i;
                    ey = j;
                }
            }
        }
        out.println(bfs());
        out.flush();
    }

    static int bfs() {
        Queue<int[]> q = new LinkedList<>();
        q.add(new int[]{sx, sy, 6, 0});
        ghp[sx][sy] = 6;
        while (!q.isEmpty()) {
            int[] t = q.poll();
            int x = t[0], y = t[1], hp = t[2], dist = t[3];
            if (x == ex && y == ey) return dist;
            for (int i = 0; i < 4; i++) {
                int nx = x + dx[i];
                int ny = y + dy[i];
                if (!isBorder(nx, ny) && g[nx][ny] != 0 && hp - 1 > 0) {
                    if (g[nx][ny] == 4) {
                        // 血槽最多走一次
                        if (ghp[nx][ny] == 0) {
                            ghp[nx][ny] = 1;
                            q.add(new int[]{nx, ny, 6, dist + 1});
                        }
                    } else if (ghp[nx][ny] < hp - 1) {
                        ghp[nx][ny] = hp - 1;
                        q.add(new int[]{nx, ny, hp - 1, dist + 1});
                    }
                }
            }
        }
        return -1;
    }

    static boolean isBorder(int x, int y) {
        return x < 1 || x > n || y < 1 || y > m;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
